package com.obviousframework.mom.taghandler;

import java.util.HashMap;
import java.util.Map;

import com.obviousframework.mom.model.Tag;
import com.obviousframework.mom.taghandler.TagHandler;

public class TagHandlerFactory {

	public static final Map<String, Class<? extends TagHandler>> handlers = new HashMap<String, Class<? extends TagHandler>>();

	private TagHandlerFactory() {
	}

	public static TagHandler getHandler(Tag tag) {
		if (tag != null) {
			return getHandler(tag.getKey());
		}
		return null;
	}

	public static TagHandler getHandler(String tagKey) {
		Class<? extends TagHandler> handlerClass = handlers.get(tagKey);
		try {
			if (handlerClass != null) {
				return handlerClass.newInstance();
			}
		} catch (Exception e) {
			// TODO Definir tratamento para essa excecao
		}
		return null;
	}

	public static void registerHandler(String tagKey, Class<? extends TagHandler> handlerClass) {
		handlers.put(tagKey, handlerClass);
	}
}
